package com.glwan.fixedassetapp.ui.home.locateTag;

import android.os.Build;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.glwan.fixedassetapp.R;
import com.glwan.fixedassetapp.handler.ReaderResponseInterfaces;
import com.glwan.fixedassetapp.helper.KeyCodeReceiver;
import com.glwan.fixedassetapp.helper.RFIDDeviceInterface;
import com.glwan.fixedassetapp.model.RFIDTagData;
import com.glwan.fixedassetapp.utils.CommonUtil;

//ListableAdapterCustomHandler
public class LocateTagActivity extends AppCompatActivity implements ReaderResponseInterfaces.ResponseStatusHandler, ReaderResponseInterfaces.ResponseTagHandler, ReaderResponseInterfaces.ResponseKeyEventHandler {

    EditText tagPattern;

    Button btn_locate;

    RangeGraph rangeGraph;

    RFIDDeviceInterface rfidDeviceInterface;

    KeyCodeReceiver keyCodeReceiver;

    @Override
    protected void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.single_tag_locate);
        Toolbar toolbar = findViewById(R.id.moreToolbar);
        setSupportActionBar(toolbar);

        setTitle(getString(R.string.locateTag));

        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
        tagPattern = findViewById(R.id.tagPattern);
        String tagPatternValue = getIntent().getStringExtra("tagPatternValue");
        tagPattern.setText(tagPatternValue);
        btn_locate = findViewById(R.id.btn_locate);
        rangeGraph = findViewById(R.id.locationBar);
        rangeGraph.setValue(0);

        btn_locate.setOnClickListener(view -> {
            inventory();
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        rfidDeviceInterface = RFIDDeviceInterface.getInstance(Build.BRAND, this);
    }

    private void inventory() {
        if (getString(R.string.startScanning).equals(btn_locate.getText().toString())) {
            if (tagPattern.getText() == null || tagPattern.getText().toString().isEmpty() || "null".equals(tagPattern.getText().toString())) {
                CommonUtil.toastShow(getActivity(), "未选择标签", CommonUtil.ERROR, true);
                return;
            }
            btn_locate.setText(getString(R.string.stopScanning));
            rfidDeviceInterface.changeInventoryMode(1, tagPattern.getText().toString());
            rfidDeviceInterface.startInventory();

        } else {
            btn_locate.setText(getString(R.string.startScanning));
            rfidDeviceInterface.stopInventory();

        }
    }

    @Override
    public void handleResponse(RFIDTagData data) {

        short relativeDistance;
        relativeDistance = data.getRelativeDistance();
        if (relativeDistance != -1) {
            rangeGraph.setValue(data.getRelativeDistance());
            rangeGraph.invalidate();
            rangeGraph.requestLayout();

        }
    }

    //返回主界面
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onResume() {
        super.onResume();
        keyCodeReceiver = KeyCodeReceiver.getInstance(this);
        keyCodeReceiver.registerKeyCodeReceiver();
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (rfidDeviceInterface != null)
            rfidDeviceInterface.stopInventory();
        if (keyCodeReceiver != null)
            keyCodeReceiver.unregisterReceiver();
        btn_locate.setText(getString(R.string.startScanning));
    }

    @Override
    public void handleResponse() {
        inventory();
    }

    private LocateTagActivity getActivity() {
        return this;
    }


    @Override
    public void handleStatusResponse(boolean open) {
        rfidDeviceInterface.changeInventoryMode(1, tagPattern.getText().toString());
    }
}
